package com.xianbeiying.app.ckgl.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.PopWindowHistoryAdapter;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.GzQdDataBean;
import com.xianbeiying.app.ckgl.bean.HistoryDataBean;
import com.xianbeiying.app.ckgl.bean.SDDataBean;
import com.xianbeiying.app.ckgl.bean.TrSdDataBean;
import com.xianbeiying.app.ckgl.bean.TrWdDataBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.data.HistoryDataUtil;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.DataUtil;
import com.xianbeiying.app.ckgl.util.StringUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.AppManager;
import com.xianbeiying.app.ckgl.widght.CustomTextView;
import com.xianbeiying.app.ckgl.widght.brokenline.LineView;
import com.xianbeiying.app.ckgl.widght.dapengView.DpView;
import com.xianbeiying.app.ckgl.widght.dapengView.Dpdata;
import com.xianbeiying.app.ckgl.widght.datetimepicker.date.DatePickerDialog;
import com.xianbeiying.app.ckgl.widght.popwindow.CenterPopWindow;
import com.xianbeiying.app.ckgl.widght.popwindow.LayoutPopWindow;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 历史数据
 */
public class HistoryDataActivityss extends BaseActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener, Dpdata, HttpCallBackUtil {
    //水平滑动的
    private LinearLayout ly_hthorizontal;
    private ImageView iv_title_left;
    private TextView tv_title;
    private View va_line;
    private TextView tv_va_all;
    private String[] contentss= {"空气温度", "空气湿度"};
//    private String[] contentss= {"空气温度", "空气湿度", "土壤温度", "土壤湿度", "光照强度",
//            "二氧化碳", "用水统计"};
    private int[] linecolor = {R.color.kqwd, R.color.kqsd, R.color.trwd, R.color.trsd, R.color.gzqd, R.color.eyht, R.color.ystj};
    private String[] strTu = {"空气温度变化趋势图", "空气湿度变化趋势图", "土壤温度变化趋势图", "土壤湿度变化趋势图",
            "光照强度变化趋势图", "二氧化碳变化趋势图", "用水统计变化趋势图"};
    private String[] x = {"℃", "RH", "℃", "RH", "Lux", "ppm", "m³"};
    private int[] imgId = {R.mipmap.fh, R.mipmap.fj, R.mipmap.gtj, R.mipmap.jk,
            R.mipmap.af, R.mipmap.fjn, R.mipmap.tyj};
    private TextView tv_wl_type, tv_time;
    private ImageView iv_wl_type;
    private Button et_his_s, et_his_e, btn_cx;
    private LayoutPopWindow historyPop;
    private GetModel getModel;
    //    private MyChartView gluc_glulay;
    //图标参数
    private TextView tv_hdate, tv_ht_tu, tv_ht_wd, tv_ht_name, tv_xunit, tv_yunit;
    private ImageView iv_line;
    private CustomTextView ct_at, ct_qt, ct_zt, ct_jt;
    private DpView dpView;
    //选择的大棚
    private int dpId;
    //选择哪种类型
    private int selectId;
    //开始时间
    private String startTiem;
    //结束时间
    private String endTime;
    //那个时间段的
    private int timeSlotId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_data);
        initView();
    }

    private LineView mSkinLineView;
    private ArrayList<Integer> colorList = new ArrayList<>();//折线的颜色列表
    private ArrayList<String> XLabel = new ArrayList<>(); //X轴上的标签数据列表
    private ArrayList<String> YLabel = new ArrayList<>(); //Y轴上的标签数据列表
    private ArrayList<ArrayList<Integer>> dataLists;//折线上的数据列表
    private ArrayList<ArrayList<Integer>> dataLists1;//折线上的数据列表
    private ArrayList<ArrayList<Integer>> dataLists2;//折线上的数据列表
    private int width;//屏幕宽度

    private void initView() {
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        ly_hthorizontal = (LinearLayout) findViewById(R.id.ly_hthorizontal);
        width = AppManager.getInstance().getWindowSizeEntity().getWidth();
        mSkinLineView = (LineView) findViewById(R.id.gluc_glulay);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_time = (TextView) findViewById(R.id.tv_time);
        va_line = (View) findViewById(R.id.va_line);
        tv_hdate = (TextView) findViewById(R.id.tv_hdate);
        tv_va_all = (TextView) findViewById(R.id.tv_va_all);
        tv_wl_type = (TextView) findViewById(R.id.tv_wl_type);
        tv_ht_tu = (TextView) findViewById(R.id.tv_ht_tu);
        tv_ht_wd = (TextView) findViewById(R.id.tv_ht_wd);
        tv_wl_type = (TextView) findViewById(R.id.tv_wl_type);
        tv_ht_name = (TextView) findViewById(R.id.tv_ht_name);
        tv_xunit = (TextView) findViewById(R.id.tv_xunit);
        tv_yunit = (TextView) findViewById(R.id.tv_yunit);
        iv_wl_type = (ImageView) findViewById(R.id.iv_wl_type);
        iv_line = (ImageView) findViewById(R.id.iv_line);
        et_his_s = (Button) findViewById(R.id.et_his_s);
        et_his_e = (Button) findViewById(R.id.et_his_e);
        btn_cx = (Button) findViewById(R.id.btn_cx);
        ct_at = (CustomTextView) findViewById(R.id.ct_at);
        ct_qt = (CustomTextView) findViewById(R.id.ct_qt);
        ct_zt = (CustomTextView) findViewById(R.id.ct_zt);
        ct_jt = (CustomTextView) findViewById(R.id.ct_jt);
        tv_time.setText(DataUtil.dateTime());
        startTiem = DataUtil.dateTime() + "  00:00:00";
        endTime = DataUtil.dateTime() + "  24:00:00";
        tv_title.setText("历史数据");
        iv_title_left.setOnClickListener(this);
        tv_va_all.setOnClickListener(this);
        iv_wl_type.setOnClickListener(this);
        et_his_s.setOnClickListener(this);
        et_his_e.setOnClickListener(this);
        btn_cx.setOnClickListener(this);
        ct_jt.setOnClickListener(this);
        ct_zt.setOnClickListener(this);
        ct_qt.setOnClickListener(this);
        ct_at.setOnClickListener(this);
        tv_ht_wd.setText("当前" + (contentss[0] + ":" + 0 + x[0]));
        ct_jt.setSelected(true);
        initData();
    }

    private void initData() {
        initChat(0, 13, 2, -2, 7, 10, "h", 15, 18);
//           initChat(-2,7,10,8,9,"h",15,18);
        if (MyApplication.devicesBean != null) {
            com.lidroid.xutils.util.LogUtils.e("---------进入路");
            devicesBean = MyApplication.devicesBean;
            getData(devicesBean.getGREENHOUSE(), devicesBean.getGREENHOUSE().size(), 0);
            getGetMdoel();
        } else {
            //获取大棚数据
            getModel.QUWEYDEVS(true);
        }
    }

    private int start;//x轴开始时间
    private int ystart;//y轴开始时间
    private int xMax;//几段
    private int yMax;
    private int jx;
    private int jy;
    private int xscle;
    private int yscale;

    private void initChat(int start, int xMax, int jx, int ystart, int yMax, int jy, String dw, int xscle, int yscale) {
        //        colorList.add(getResources().getColor(linecolor[0]));
//        colorList.add(getResources().getColor(linecolor[0]));
//        LineView.Yscale=width/18; //y轴间距
//        LineView.Xscale=width/15;//x轴间距
        LineView.Yscale = width / yscale; //y轴间距
        LineView.Xscale = width / xscle;//x轴间距
        XLabel.clear();
        YLabel.clear();
        //-10
        for (int i = start; i < xMax; i++) {
            XLabel.add(String.valueOf(i * jx));
        }
        for (int i = ystart; i < yMax; i++) {
            YLabel.add(String.valueOf(i * jy));
        }
//        mSkinLineView.setDataColorList(colorList);
        mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
        mSkinLineView.setShowGrid(true);//true是展示表格,false是显示带箭头的X,Y轴
//        mSkinLineView.setDottedLine(true); // 表格展示微虚线
        mSkinLineView.setXYColor(Color.LTGRAY); // X,Y轴线与数据的颜色
        mSkinLineView.setGridColor(Color.WHITE);// 表格的颜色
       /* mSkinLineView.setScale(32);//表格中的正方形的单位,默认是32
        mSkinLineView.setXTextSize(30);  // X轴字体的颜色
        mSkinLineView.setYTextSize(30); // Y轴字体的颜色
        mSkinLineView.setXToXTextSpace(20);
        mSkinLineView.setYToYTextSpace(40);
        mSkinLineView.setXTextColor(Color.RED);
        mSkinLineView.setYTextColor(Color.RED);
        mSkinLineView.setDataColor(Color.BLUE);*/
        tv_yunit.setText("(" + dw + ")");
    }
   private ArrayList<Integer> historyData=new ArrayList<>();
    private void randSet(int postion, ArrayList<Integer> historyDatass) {
        dataLists = new ArrayList<>();
        dataLists.clear();
        colorList.clear();
        colorList.add(getResources().getColor(linecolor[postion]));
        ArrayList<Integer> dataList1 = new ArrayList<Integer>();
//        int random1 = (int) (Math.random() * 60 + 1);i
        if(historyDatass!=null&&historyDatass.size()!=0){
            for (int i = 0; i < XLabel.size(); i++) {
                if(XLabel.size()>historyDatass.size()){
                     if((i+1)==historyDatass.size()){
                         dataList1.add(historyDatass.get(i));
                     }else{
                         dataList1.add(0);
                     }
                }else{
                    dataList1.add(historyDatass.get(i));
                }

            }
        }else{
            for (int i = 0; i < XLabel.size(); i++) {
                dataList1.add(0);
            }
        }
        dataLists.add(dataList1);
        mSkinLineView.setDataColorList(colorList);
        mSkinLineView.setDataList(dataLists);
    }

    private void randSetss(int postion, ArrayList<Integer> historyDatass) {
        dataLists = new ArrayList<>();
        dataLists.clear();
        colorList.clear();
        colorList.add(getResources().getColor(linecolor[postion]));
        dataLists.add(historyDatass);
        mSkinLineView.setDataColorList(colorList);
        mSkinLineView.setDataList(dataLists);
    }
    private void randSetsss(int postion, ArrayList<Integer> historyDatass, ArrayList<Integer> historyDatass1, ArrayList<Integer> historyDatass2) {
        dataLists = new ArrayList<>();
        dataLists1=new ArrayList<>();
        dataLists2=new ArrayList<>();
        dataLists.clear();
        colorList.clear();
        dataLists1.clear();
        dataLists2.clear();
        colorList.add(getResources().getColor(linecolor[postion]));
        dataLists.add(historyDatass);
        dataLists1.add(historyDatass1);
        dataLists2.add(historyDatass2);
        mSkinLineView.setDataColorList(colorList);
        mSkinLineView.setDataList(dataLists);
        mSkinLineView.setDataList1(dataLists1);
        mSkinLineView.setDataList2(dataLists2);
    }
    private void getData(List<DevicesBean.GREENHOUSEBean> DEVICES, int size, final int id) {
        dpView = new DpView(DEVICES, size, id, ly_hthorizontal, this);
        dpView.setDpdata();
        dpView.addResponseListener(this);
    }

    private PopWindowHistoryAdapter popWindowHistoryAdapter;
    private CenterPopWindow manulstarttime, manulendtime;
    private int sid = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //今天
            case R.id.ct_jt:
                ShowDefault();
                timeSlotId = 0;
                ct_jt.setSelected(true);
                startTiem = DataUtil.dateTime() + "  00:00:00";
                endTime = DataUtil.dateTime() + "  24:00:00";
//                initChat(0,13,2,-2,7,10,"h",15,18);
                getGetMdoel();
//                randSet(0);
//                getModel.GETHISTORY("",124,"",false);
                tv_time.setText(DataUtil.dateTime());
                break;
            //昨天
            case R.id.ct_zt:
                ShowDefault();
                timeSlotId = 1;
                ct_zt.setSelected(true);
                startTiem = DataUtil.getLastTime(-1) + "  00:00:00";
                endTime = DataUtil.getLastTime(-1) + "  24:00:00";
//                initChat(0,13,2,-2,7,10,"h",15,18);
                tv_time.setText( DataUtil.getLastTime(-1));
                getGetMdoel();
//                randSet(0);
//                getModel.GETHISTORY("",124,"",false);
                break;
            //七天
            case R.id.ct_qt:
                ShowDefault();
                timeSlotId = 2;
                ct_qt.setSelected(true);
                startTiem = DataUtil.getLastTime(-6) + "  00:00:00";
                endTime = DataUtil.dateTime() + "  24:00:00";
                tv_time.setText( DataUtil.getLastTime(-6)+"至"+DataUtil.dateTime());
                getGetMdoel();
//                randSet(0);
//                getModel.GETHISTORY("",124,"",false);
                break;
            //三十天
            case R.id.ct_at:
                ShowDefault();
                timeSlotId = 3;
                ct_at.setSelected(true);
                startTiem = DataUtil.getLastTime(-29) + "  00:00:00";
                endTime = DataUtil.dateTime() + "  24:00:00";
                tv_time.setText( DataUtil.getLastTime(-29)+"至"+DataUtil.dateTime());
                getGetMdoel();
//                randSet(0);
//                getModel.GETHISTORY("",124,"",false);
                break;
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_va_all:
                tv_va_all.setVisibility(View.GONE);
                va_line.setVisibility(View.GONE);
                break;
            case R.id.iv_wl_type:
                historyPop = new LayoutPopWindow(this, R.layout.pop_window_history, tv_wl_type);
                ListView mlistView = historyPop.getView(R.id.pop_history_lists);
                if (popWindowHistoryAdapter == null) {
                    popWindowHistoryAdapter = new PopWindowHistoryAdapter(this, contentss, imgId);
                    popWindowHistoryAdapter.setIsBoolean(StringUtil.getArryList(contentss));
                }
                mlistView.setAdapter(popWindowHistoryAdapter);
                mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        int size = popWindowHistoryAdapter.getIsBoolean().size();
                        for (int i = 0; i < size; i++) {
                            if (i == position) {
                                popWindowHistoryAdapter.getIsBoolean().set(position, true);
                            } else {
                                popWindowHistoryAdapter.getIsBoolean().set(i, false);
                            }
                        }
                        popWindowHistoryAdapter.notifyDataSetChanged();
                        tv_wl_type.setText(contentss[position]);
                        tv_ht_tu.setText(contentss[position] + "趋势图");
                        tv_ht_name.setText(contentss[position]);
                        tv_ht_wd.setText("当前" + (contentss[position] + ":" + 0 + x[position]));
                        tv_xunit.setText(x[position]);
                        tv_hdate.setText(strTu[position]);
                        iv_line.setImageResource(imgId[position]);
//                        randSet(position);
                        selectId = position;
                        getGetMdoel();
                        historyPop.dismiss();
                    }
                });
                break;
            //选择
            case R.id.et_his_s:
                sid = 0;
                et_his_s.setTag("开始");
                getTime();
                break;
            case R.id.et_his_e:
                sid = 1;
                et_his_e.setTag("结束");
                if(et_his_s.getText().toString().trim().isEmpty()){
                    ToastUtil.showInfo(HistoryDataActivityss.this,"请先选择前面时间");
                }else{
                    getTime();
                }
                break;
            //查询
            case R.id.btn_cx:
                timeSlotId = 4;
                ct_jt.setSelected(false);
                ct_zt.setSelected(false);
                ct_qt.setSelected(false);
                ct_at.setSelected(false);
                if (et_his_s.getText().toString().isEmpty()) {
                    ToastUtil.showInfo(HistoryDataActivityss.this, "请选择时间");
                    return;
                }
                if (et_his_e.getText().toString().isEmpty()) {
                    ToastUtil.showInfo(HistoryDataActivityss.this, "请选择时间");
                    return;
                }
                startTiem = et_his_s.getText().toString() + "  00:00:00";
                endTime = et_his_e.getText().toString() + "  24:00:00";
                number = Integer.valueOf(DataUtil.getHHLong(startTiem, endTime));
                if (number > 31){
                    ToastUtil.showInfo(HistoryDataActivityss.this, "选择范围是在30天之内");
                    return;
                }
                LogUtils.e("-----"+number);
                if(number==1){
                    timeSlotId =1;
                }
                getGetMdoel();
                break;
        }
    }

    private int number;

    private void getGetMdoel() {
        //空气温度
        if (selectId == 0) {
            setXY(-2, 11, 10);
            getModel.GET_AIR_TEMP("GET_AIR_TEMP", devicesBean.getGREENHOUSE().get(dpId).getGreenhouseId(), startTiem, endTime, true);
        }
        //空气湿度
        if (selectId == 1) {
            setXY(0, 13, 10);
            getModel.GET_AIR_HUMIDITY("GET_AIR_HUMIDITY", devicesBean.getGREENHOUSE().get(dpId).getGreenhouseId(), startTiem, endTime, true);
        }
        //土壤温度
        if (selectId == 2) {
            setXY(-2,11, 10);
            getModel.GET_SOIL_TEMP("GET_SOIL_TEMP", devicesBean.getGREENHOUSE().get(dpId).getGreenhouseId(), startTiem, endTime, true);
        }
        //土壤湿度
        if (selectId == 3) {
            setXY(0, 13, 10);
            getModel.GET_SOIL_HUMIDITY("GET_SOIL_HUMIDITY", devicesBean.getGREENHOUSE().get(dpId).getGreenhouseId(), startTiem, endTime, true);
        }
        //光照强度
        if (selectId == 4) {
            setXY(0, 13, 1000);
            getModel.GET_BEAM_STRENGTH("GET_BEAM_STRENGTH", devicesBean.getGREENHOUSE().get(dpId).getGreenhouseId(), startTiem, endTime, true);
        }
        //二氧化碳
        if (selectId == 5){

        }
        //用水统计
        if (selectId == 6){

        }
    }

    //设置xy轴的刻度数据
    private void setXY(int ystart, int yMax, int yjian) {
        //今天
        if (timeSlotId == 0) {
            initChat(0, 13, 2, ystart, yMax, yjian, "h", 15, 18);
        }
        //昨天
        if (timeSlotId == 1) {
            initChat(0, 13, 2, ystart, yMax, yjian, "h", 15, 18);
        }
        //七天
        if (timeSlotId == 2) {
            initChat(1, 8, 1, ystart, yMax, yjian, "号", 8, 18);
        }
        //三十天
        if (timeSlotId == 3) {
            initChat(1, 11, 3, ystart, yMax, yjian, "号", 12, 18);
        }
        //查询
        if (timeSlotId == 4) {
            if (0 < number && number < 16) {
                initChat(1, number + 1, 1, ystart, yMax, yjian, "号", number + 2, 18);
            } else if (16 <= number && number <= 26) {
                    initChat(1, number / 2 + 1, 2, ystart, yMax, yjian, "号", number / 2 + 2, 18);
            } else{
                    initChat(1, number / 3 + 1, 3, ystart, yMax, yjian, "号", number / 3 + 2, 18);
            }
        }
    }


    private void ShowDefault() {
        ct_jt.setSelected(false);
        ct_zt.setSelected(false);
        ct_qt.setSelected(false);
        ct_at.setSelected(false);
        et_his_s.setText("");
        et_his_e.setText("");
    }

    private void getTime() {
        final Calendar calendar = Calendar.getInstance();
        final DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), true);
        datePickerDialog.setYearRange(1985, 2028);
        datePickerDialog.setCloseOnSingleTapDay(true);
        datePickerDialog.show(getSupportFragmentManager(), "");
    }

    @Override
    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day){
        int endTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(year + "-" + (month + 1) + "-" + day,"yyyy-MM-dd");
        int downTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(com.xianbeiying.app.ckgl.util.DataUtil.dateHTime(),"yyyy-MM-dd");
        if (sid == 0) {
            if(endTime>downTime){
                ToastUtil.showInfo(HistoryDataActivityss.this,"选择时间不能大于当前时间");
                return;
            }
            et_his_s.setText(year + "-" + (month + 1) + "-" + day);
        }
        if (sid == 1) {
            if(endTime>downTime){
                ToastUtil.showInfo(HistoryDataActivityss.this,"选择时间不能大于当前时间");
                return;
            }
            if(!et_his_s.getText().toString().trim().isEmpty()){
                int starttime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(et_his_s.getText().toString().trim(),"yyyy-MM-dd");
                if(starttime>endTime){
                    ToastUtil.showInfo(HistoryDataActivityss.this,"前面时间不能大于后面时间");
                    return;
                }
                et_his_e.setText(year + "-" + (month + 1) + "-" + day);
            }
        }
        if (!et_his_s.getText().toString().isEmpty() && !et_his_e.getText().toString().isEmpty()) {
            tv_time.setText(et_his_s.getText().toString() + "至" + et_his_e.getText().toString());
        }
    }

    @Override
    public void onDpdata(int sid, boolean isChoice) {
        if (isChoice) {
            dpId = sid;
            getGetMdoel();
        }
//        getModel.GETHISTORY("",124,"",false);
    }

    //网络返回值
    private DevicesBean devicesBean;
    private HistoryDataBean historyDataBean;//空气温度
    private SDDataBean sdbean;//空气湿度
    private TrWdDataBean trWdbean;//土壤温度
    private TrSdDataBean trdatebean;//土壤湿度
    private GzQdDataBean gzQdbean;//光照强度
    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals(UrlBean.QUWEYDEVS)) {
            if (content != null) {
                devicesBean = JSON.parseObject(new String((byte[]) content), DevicesBean.class);
                MyApplication.devicesBean = devicesBean;
                getData(devicesBean.getGREENHOUSE(), devicesBean.getGREENHOUSE().size(), 0);
                getGetMdoel();
            }
        }
        if (url.equals("空气温度")) {
            if (content != null) {
                historyDataBean = JSON.parseObject(new String((byte[]) content), HistoryDataBean.class);
                historyData.clear();
                if (historyDataBean.isR()){
                    if (historyDataBean.getV().getAIR_TEMP_DATA()!= null &&historyDataBean.getV().getAIR_TEMP_DATA().size()!= 0) {
                        if(timeSlotId==0||timeSlotId==1){//今天或者昨天的数据展示
                            mSkinLineView.setList(false);
                            ArrayList<Integer> dats=HistoryDataUtil.getOneKqsdModel(XLabel,historyDataBean);
                            randSetss(selectId,dats);
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[0] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[0]);
                        }
                        if(timeSlotId==2){//七天或者三十天的数据展示
                            mSkinLineView.setList(true);
                           XLabel=HistoryDataUtil.getQXLabel(7,1);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-6).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[0] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[0]);
                        }
                        if(timeSlotId==3){
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(30,3);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-29).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[0] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[0]);
                        }
                        if(timeSlotId==4){//查询的
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getCXXLabel(et_his_s.getText().toString(),number);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(et_his_s.getText().toString().split("-")[1]);
                            int end=Integer.valueOf(et_his_e.getText().toString().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean),
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[0] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[0]);
                        }
                    }
                }else{
                    ToastUtil.showInfo(HistoryDataActivityss.this,historyDataBean.getERR_MSG());
                }
            }
        }
        if (url.equals("空气湿度")) {
            if (content != null) {
                sdbean = JSON.parseObject(new String((byte[]) content), SDDataBean.class);
                historyData.clear();
                if (sdbean.isR()) {
                    if (sdbean.getV().getAIR_HUMIDITY_DATA()!= null &&sdbean.getV().getAIR_HUMIDITY_DATA().size()!= 0) {
                        historyDataBean=HistoryDataUtil.getData1(sdbean);
                        if(timeSlotId==0||timeSlotId==1){//今天或者昨天的数据展示
                            mSkinLineView.setList(false);
                            ArrayList<Integer> dats=HistoryDataUtil.getOneKqsdModel(XLabel,historyDataBean);
                            randSetss(selectId,dats);
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[1] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[1]);
                        }
                        if(timeSlotId==2){//七天或者三十天的数据展示
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(7,1);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-6).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[1] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[1]);
                        }
                        if(timeSlotId==3){
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(30,3);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-29).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[1] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[1]);
                        }
                        if(timeSlotId==4){//查询的
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getCXXLabel(et_his_s.getText().toString(),number);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(et_his_s.getText().toString().split("-")[1]);
                            int end=Integer.valueOf(et_his_e.getText().toString().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean),
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[1] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[1]);
                        }
                    }
                }else{
                    ToastUtil.showInfo(HistoryDataActivityss.this,sdbean.getERR_MSG());
                }
            }
        }
        if (url.equals("土壤温度")) {
            if (content != null) {
                trWdbean = JSON.parseObject(new String((byte[]) content), TrWdDataBean.class);
                historyData.clear();
                if (trWdbean.isR()) {
                    if (trWdbean.getV().getSOIL_TEMP_DATA()!= null &&trWdbean.getV().getSOIL_TEMP_DATA().size()!= 0) {
                        historyDataBean=HistoryDataUtil.getData2(trWdbean);
                        if(timeSlotId==0||timeSlotId==1){//今天或者昨天的数据展示
                            mSkinLineView.setList(false);
                            ArrayList<Integer> dats=HistoryDataUtil.getOneKqsdModel(XLabel,historyDataBean);
                            randSetss(selectId,dats);
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[2] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[2]);
                        }
                        if(timeSlotId==2){//七天或者三十天的数据展示
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(7,1);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-6).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[2] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[2]);
                        }
                        if(timeSlotId==3){
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(30,3);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-29).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[2] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[2]);
                        }
                        if(timeSlotId==4){//查询的
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getCXXLabel(et_his_s.getText().toString(),number);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(et_his_s.getText().toString().split("-")[1]);
                            int end=Integer.valueOf(et_his_e.getText().toString().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean),
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[2] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[2]);
                        }
                    }
                }else{
                    ToastUtil.showInfo(HistoryDataActivityss.this,trWdbean.getERR_MSG());
                }
            }
        }
        if (url.equals("土壤湿度")){
            if (content != null) {
                trdatebean=JSON.parseObject(new String((byte[]) content), TrSdDataBean.class);
                historyData.clear();
                if (trdatebean.isR()){
                    if (trdatebean.getV().getSOIL_HUMIDITY_DATA()!= null &&trdatebean.getV().getSOIL_HUMIDITY_DATA().size()!= 0) {
                        historyDataBean=HistoryDataUtil.getData3(trdatebean);
                        if(timeSlotId==0||timeSlotId==1){//今天或者昨天的数据展示
                            mSkinLineView.setList(false);
                            ArrayList<Integer> dats=HistoryDataUtil.getOneKqsdModel(XLabel,historyDataBean);
                            randSetss(selectId,dats);
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[3] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[3]);
                        }
                        if(timeSlotId==2){//七天或者三十天的数据展示
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(7,1);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-6).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[3] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[3]);
                        }
                        if(timeSlotId==3){
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(30,3);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-29).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[3] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[3]);
                        }
                        if(timeSlotId==4){//查询的
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getCXXLabel(et_his_s.getText().toString(),number);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(et_his_s.getText().toString().split("-")[1]);
                            int end=Integer.valueOf(et_his_e.getText().toString().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean),
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[3] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[3]);
                        }
                    }
//                    randSet(selectId, historyData);
                }else{
                    ToastUtil.showInfo(HistoryDataActivityss.this,trdatebean.getERR_MSG());
                }
            }
        }
        if (url.equals("光照强度")) {
            if (content != null) {
                gzQdbean = JSON.parseObject(new String((byte[]) content), GzQdDataBean.class);
                historyData.clear();
                if (gzQdbean.isR()) {
                    if (gzQdbean.getV().getBEAM_DATA()!= null &&gzQdbean.getV().getBEAM_DATA().size()!= 0) {
                        historyDataBean=HistoryDataUtil.getData4(gzQdbean);
                        if(timeSlotId==0||timeSlotId==1){//今天或者昨天的数据展示
                            mSkinLineView.setList(false);
                            YLabel=HistoryDataUtil.getCYXLabel(1,gzQdbean);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            ArrayList<Integer> dats=HistoryDataUtil.getOneKqsdModel(XLabel,historyDataBean);
                            randSetss(selectId,dats);
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[4] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[4]);
                        }
                        if(timeSlotId==2){//七天或者三十天的数据展示
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(7,1);
                            YLabel=HistoryDataUtil.getCYXLabel(2,gzQdbean);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-6).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[4] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[4]);
                        }
                        if(timeSlotId==3){
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getQXLabel(30,3);
                            YLabel=HistoryDataUtil.getCYXLabel(2,gzQdbean);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(DataUtil.getLastTime(-29).split("-")[1]);
                            int end=Integer.valueOf(DataUtil.dateTime().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,dats,
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[4] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[4]);
                        }
                        if(timeSlotId==4){//查询的
                            mSkinLineView.setList(true);
                            XLabel=HistoryDataUtil.getCXXLabel(et_his_s.getText().toString(),number);
                            YLabel=HistoryDataUtil.getCYXLabel(2,gzQdbean);
                            mSkinLineView.setXYLabel(XLabel, YLabel); //设置设置X,Y轴的数据
                            int start=Integer.valueOf(et_his_s.getText().toString().split("-")[1]);
                            int end=Integer.valueOf(et_his_e.getText().toString().split("-")[1]);
                            ArrayList<Integer> dats=HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean);
                            randSetsss(selectId,HistoryDataUtil.getQAvgKqsdModel(XLabel,start,end,historyDataBean),
                                    HistoryDataUtil.getQMaxKqsdModel(XLabel,start,end,historyDataBean),HistoryDataUtil.getQMinKqsdModel(XLabel,start,end,historyDataBean));
                            int size=historyDataBean.getV().getAIR_TEMP_DATA().size();
                            tv_ht_wd.setText("当前" +contentss[4] +":"+(int)historyDataBean.getV().getAIR_TEMP_DATA().get(size-1).getDATA()+x[4]);
                        }
                    }
//                    randSet(selectId, historyData);
                }else{
                    ToastUtil.showInfo(HistoryDataActivityss.this,gzQdbean.getERR_MSG());
                }
            }
        }
    }
}
